<style scoped>
</style>

<template>
  <div :class="dclass">
    <div>
      <p-input
        ref="input"
        v-model.lazy="modelValue"
        :name="name"
        :type="_type"
        :class="inputClass"
        :color="inputColor"
        :value="value"
        :disabled="disabled"
        @change="event => { $emit('change', event) }"
      >
        <slot></slot>
      </p-input>
    </div>
    <div class="pointer" @click="select()">
      <label v-if="hasSlot('label')" class="pointer" :for="formid">
        <slot name="label"></slot>
      </label>
      <slot name="extra"></slot>
    </div>
  </div>
</template>

<script>
import PInput from 'pretty-checkbox-vue/input';

export default {

  components: {
    PInput
  },

  model: {
    prop: 'modelValue',
    event: 'change'
  },

  props: {
    name: {
      type: String,
      default: '',
    },
    value: {
      type: [String, Boolean],
      default: '',
    },
    modelValue: {
      type: [String, Boolean],
      default: '',
    },
    iclass: {
      type: [String, Array],
      default: ''
    },
    dclass: {
      type: [String, Array],
      default: ''
    },
    color: {
      type: [String, Array],
      default: ''
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },

  computed: {
    _type() {
      if (this.$options.input_type) {
        return this.$options.input_type;
      }
      return 'input';
    },
    inputClass() {
      return [this.iclass, 'p-default', this.$options.input_iclass];
    },
    inputColor() {
      return this.color != '' ? this.color : 'primary-o';
    },
  },

  methods: {
    formid() {
      return this.$refs.input.id;
    },
    select() {
      switch (this._type)
      {
      case 'checkbox':
        this.$refs.input.$refs.input.checked = ! this.$refs.input.$refs.input.checked;
        this.$emit('change', this.$refs.input.$refs.input.checked);
        break;
      case 'radio':
        this.$refs.input.$refs.input.checked = true;
        this.$emit('change', this.value);
        break;
      case 'input':
          //this.$refs.input.$refs.input.focus();
      }
    },
    hasSlot (name = 'default') {
      return !!this.$slots[ name ] || !!this.$scopedSlots[ name ];
    }
  }
};
</script>
